// 全局存储的：
// 解锁标记：$gameSystem._enemyBookFlags
//   击杀数：$gameSystem._defeatNumbers【不存储】
//   辨识度：$gameSystem._identify
/////////////////////////////////////////////////////////////////////
// DataManager
/////////////////////////////////////////////////////////////////////
// 读取神魔录信息（抄的global）
DataManager.loadGlobalEnemyBookInfo = function() {
    var globalEnemySaveId = -2
    var json;
    try {
        json = StorageManager.load(globalEnemySaveId);
    } catch (e) {
        console.error(e);
        return [];
    }
    if (json) {
        var enemyBook = JsonEx.parse(json);
        return enemyBook;
    } else {
        return [];
    }
};

// 存储神魔录（直接写进handleData里了）
/*
const SWDA_DataManager_saveGameWithoutRescue = DataManager.saveGameWithoutRescue;
DataManager.saveGameWithoutRescue = function (savefileId) {
    const result = SWDA_DataManager_saveGameWithoutRescue.call(this, savefileId)
    this.saveGlobalEnemyBook();
    return result;
};
*/

// 读取存档时，读取神魔录
const SWDA_DataManager_loadGameWithoutRescue = DataManager.loadGameWithoutRescue;
DataManager.loadGameWithoutRescue = function(savefileId) {
    const result = SWDA_DataManager_loadGameWithoutRescue.call(this, savefileId)
    if(result)
        this.loadGlobalEnemyBook();
    return result;
};

// 开始新游戏时，读取神魔录
const SWDA_DataManager_selectSavefileForNewGame = DataManager.selectSavefileForNewGame;
DataManager.selectSavefileForNewGame = function() {
    SWDA_DataManager_selectSavefileForNewGame.call(this);
    this.loadGlobalEnemyBook();
};


// 存储神魔录（存档编号-2）
DataManager.saveGlobalEnemyBook = function() {
    const globalEnemySaveId = -2;
    let enemyBook = JsonEx.stringify(this.makeGlobalEnemyBook());
    StorageManager.save(globalEnemySaveId, enemyBook);
};

DataManager.loadGlobalEnemyBook = function(){
    let enemyBook = this.loadGlobalEnemyBookInfo();
    if(enemyBook.length !== 0)
        this.extractGlobalEnemyBook(enemyBook);

}

// 生成存档文件（存档文件存储的内容）
DataManager.makeGlobalEnemyBook = function() {
    let enemyBook = {};
    enemyBook.enemyBookFlags     = $gameSystem._enemyBookFlags;
    //enemyBook.defeatNumbers      = $gameSystem._defeatNumbers;
    enemyBook.identify           = $gameSystem._identify;
    return enemyBook;
};

// 提取存档中的内容
DataManager.extractGlobalEnemyBook = function(enemyBook) {
    // 不能存在时，直接读取覆盖
    if(!$gameSystem._enemyBookFlags || !$gameSystem._defeatNumbers || !$gameSystem._identify){
        $gameSystem._enemyBookFlags = enemyBook.enemyBookFlags;
        //$gameSystem._defeatNumbers  = enemyBook.defeatNumbers;
        $gameSystem._identify       = enemyBook.identify;
    // 存在时，更新为最全的
    }else{
        $gameSystem._enemyBookFlags = this.enemyBookCombineOr($gameSystem._enemyBookFlags, enemyBook.enemyBookFlags)
        //$gameSystem._defeatNumbers = this.enemyBookCombineMax($gameSystem._defeatNumbers, enemyBook.defeatNumbers)
        $gameSystem._identify = this.enemyBookCombineMax($gameSystem._identify, enemyBook.identify)
    }
};

//解锁状态取或
DataManager.enemyBookCombineOr = function(arr1, arr2){
    const maxLength = Math.max(arr1.length, arr2.length);   // 取最长的
    let result = [];
    for(let i = 0; i < maxLength; i++){
        if(arr1[i] || arr2[i])
            result[i] = true;
        else
            result[i] = null;
    }
    return result;
}

//辨识度或击杀数取最大值
DataManager.enemyBookCombineMax = function(arr1, arr2){
    const maxLength = Math.max(arr1.length, arr2.length);   // 取最长的
    let result = [];
    for(let i = 0; i < maxLength; i++){
        if((arr1[i] !=undefined) && (arr2[i] !=undefined )){
            result[i] = Math.max(arr1[i],arr2[i]);
        }else if ((arr1[i] == undefined) && (arr2[i] != undefined )){
            result[i] = arr2[i];
        }else if ((arr1[i] != undefined) && (arr2[i] == undefined )){
            result[i] = arr1[i];
        }else{
            result[i] = null;
        }
    }
    return result;
}

/////////////////////////////////////////////////////////////////////
// StorageManager
/////////////////////////////////////////////////////////////////////
// 补充神魔录存档
const SWDA_StorageManager_localFilePath = StorageManager.localFilePath;
StorageManager.localFilePath = function(savefileId) {
    let name;
    if (savefileId < -1) {
        name = 'enemyBook.rpgsave';
        return this.localFileDirectoryPath() + name;
    }else if (savefileId === -1) {
        name = 'config.rpgsave';
        return this.localFileDirectoryPath() + name;
    } else{
        return SWDA_StorageManager_localFilePath.call(this, savefileId)
    }
};

// 补充神魔录存档
const SWDA_StorageManager_webStorageKey = StorageManager.webStorageKey;
StorageManager.webStorageKey = function(savefileId) {
    if (savefileId < -1) {
        return 'RPG EnemyBook';
    }else if (savefileId === -1) {
        return 'RPG Config';
    }else{
        return SWDA_StorageManager_webStorageKey.call(this, savefileId)
    }
};